package com.apps.ftapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MySubjectCollAdapter;
import com.apps.ftapp.model.MyCollTypeModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

/**
 * 我的收藏题目
 */
public class MySubjectCollActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private Gson gson = new Gson();

    private List<MyCollTypeModel.DataBean> dataList = new ArrayList();
    private MySubjectCollAdapter mySubjectCollAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_subject_coll);
        ButterKnife.bind(this);

        setTitle("题目收藏");

        initView();

        loadData();

    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("my/favorite/question/show", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));
                    MyCollTypeModel result = new MyCollTypeModel();
                    try {
                        result = gson.fromJson(new String(responseBody), MyCollTypeModel.class);
                    } catch (Exception e) {

                    }
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        dataList.addAll(result.getData());
                        mySubjectCollAdapter.addRes(result.getData());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));
            }
        });


    }


    private void initView() {


        mySubjectCollAdapter = new MySubjectCollAdapter(getApplicationContext(), dataList);
        listView.setAdapter(mySubjectCollAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                HashMap<String, Object> data = new HashMap<String, Object>();
                data.put("id", dataList.get(i).getId());
                data.put("name", dataList.get(i).getName());
                data.put("type", dataList.get(i).getType());

                ActivitySkipUtil.skipAnotherActivity(MySubjectCollActivity.this, MySubjectCollListActivity.class, data);
            }
        });
    }
}
